package com.jianyun.jyzs.http;

import android.util.Log;

import com.google.gson.Gson;
import com.jianyun.jyzs.bean.EngineerAgreeResult;
import com.jianyun.jyzs.bean.EngineerDiscussResult;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.dao.EngineerDao;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.RceLog;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by mlli on 2017/10/25.
 */

public class EngineerHttpHelper {


    public interface AgreeResult {
        void agreeResult(boolean b, String msg, List<EngineeringResult.EngineerBean.AgressBean> list);
    }

    public interface DiscussResult {
        void discussResult(boolean b, String msg, List<EngineeringResult.EngineerBean.DiscussBean> list);
    }



    /**
     * 点赞
     *
     * @param dao
     * @param projectId
     * @param projectProgressId
     * @param agreeResult
     */
    public static void agree(final EngineerDao dao, String projectId, final String projectProgressId, final AgreeResult agreeResult) {

        HashMap<String, String> map = new HashMap<>();
        final String enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        String userId = LoginCache.getInstance().getUserInfo().getUserId();
        String name = LoginCache.getInstance().getUserDetaileInfo().getName();
        try {
            name = URLEncoder.encode(name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        map.put("method", "SendProjectProgressAgree");
        map.put("enterpriseCode", enterpriseCode);
        map.put("userId", userId);
        map.put("userName", name + ",");
        map.put("projectId", projectId);
        map.put("projectProgressId", projectProgressId);
        map.put("appCode", "jyzs");
        map.put("appType", "Android");
        map.put("appVersion", "1.0.2");
        Api api = RetrofitHelper.builderCeshiHttp().create(Api.class);
        api.newagreeEngineer(map).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<EngineerAgreeResult>() {
                    @Override
                    protected void onResponse(EngineerAgreeResult response) {
                        if (response.isResult()) {
                            //修改本地数据库的点赞数据
                            EngineeringResult.EngineerBean bean = dao.getEngineerByid(projectProgressId, enterpriseCode);
                            if (bean != null) {
                                String toJson = new Gson().toJson(response.getAgress());
                                bean.setListAgress(toJson);
                                dao.insert(bean);
                            }
                            List<EngineeringResult.EngineerBean.AgressBean> list = response.getAgress();
                            agreeResult.agreeResult(true, null, list);
                        } else {
                            agreeResult.agreeResult(false, response.getMessage(), null);
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        agreeResult.agreeResult(false, msg, null);
                    }
                });

    }

    /**
     * 发出评论
     *
     * @param dao
     * @param map
     * @param pid
     * @param discussResult
     */
    public static void discuss(final EngineerDao dao, final HashMap<String, String> map, final String pid, final DiscussResult discussResult) {
        final String ENCODE = "utf-8";
        final String enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        String userId = LoginCache.getInstance().getUserInfo().getUserId();
        String name = LoginCache.getInstance().getUserDetaileInfo().getName();
        String textA = map.get("text");
        String enName = "";
        String text = "";
        String toUserName = "";
        try {
            String toUserNameStr = map.get("toUserName");
            if (toUserNameStr == null) {
                map.remove("toUserName");
            } else {
                toUserName = URLEncoder.encode(toUserNameStr, ENCODE);
                map.put("toUserName", toUserName);
            }
            enName = URLEncoder.encode(name, ENCODE);
            text = URLEncoder.encode(textA, ENCODE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("method", "SendProjectProgressDiscuss");
        map.put("enterpriseCode", enterpriseCode);
        map.put("fromUserId", userId);
        map.put("fromUserName", enName + ",");
        map.put("text", text);
        map.put("appCode", "jyzs");
        map.put("appType", "Android");
        map.put("appVersion", "1.0.2");
        if (map.get("toUserId") == null) map.remove("toUserId");

        Api api = RetrofitHelper.builderCeshiHttp().create(Api.class);
        api.newdiscussEngineerDynamic(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<EngineerDiscussResult>() {
                    @Override
                    protected void onResponse(EngineerDiscussResult response) {
                        if (response.isResult()) {
                            EngineeringResult.EngineerBean bean = dao.getEngineerByid(pid, enterpriseCode);
                            if (bean != null) {
                                List<EngineeringResult.EngineerBean.DiscussBean> discuss = response.getDiscuss();
                                String toJson = new Gson().toJson(reverse(discuss));
                                bean.setListDisscuss(toJson);
                                dao.insert(bean);
                            }
                            discussResult.discussResult(true, null, response.getDiscuss());
                        } else {
                            discussResult.discussResult(false, response.getMessage(), null);
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        discussResult.discussResult(false, msg, null);
                    }
                });

    }

    public static List<EngineeringResult.EngineerBean.DiscussBean> reverse(List<EngineeringResult.EngineerBean.DiscussBean> discuss) {
        List<EngineeringResult.EngineerBean.DiscussBean> list = new ArrayList<>();
        for (int i = discuss.size() - 1; i >= 0; i--) {
            list.add(discuss.get(i));
        }
        return list;
    }
}
